package com.qingyun.video.service.impl;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingyun.apis.user.IUserClient;
import com.qingyun.common.constants.VideoMessageConstants;
import com.qingyun.model.common.dtos.R;
import com.qingyun.model.user.pojos.User;
import com.qingyun.model.video.dtos.PublishDto;
import com.qingyun.model.video.pojos.Video;
import com.qingyun.model.video.pojos.VideoChannel;
import com.qingyun.video.mapper.PublishMapper;
import com.qingyun.video.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.*;


@Service
@Slf4j
@Transactional
public class PublishServiceImpl extends ServiceImpl<PublishMapper, Video> implements PublishService {







    @Resource
    private VideoChannelService videoChannelService;

    @Resource
    private VideoTaskService videoTaskService;

    @Lazy
    @Resource
    private VideoService videoService;




    /**
     * 发布修改文章或保存为草稿
     * @param dto
     * @return
     */
    @Override
    public R submitNews(PublishDto dto) {

        //0.条件判断
        if(dto == null || dto.getContent() == null){
            return R.error("非法参数");
        }

        //1.保存或修改文章

        Video video = new Video();
        //属性拷贝 属性名词和类型相同才能拷贝
        BeanUtils.copyProperties(dto,video);//拷贝到文章表



        saveOrUpdateVideo(video);
        //2.判断是否为草稿  如果为草稿结束当前方法
        if(dto.getStatus().equals(Video.Status.NORMAL.getCode())){
            return R.success("保存成功");
        }

        R responseResult = videoService.saveVideo(video);
        if (!responseResult.getCode().equals(1)){
            throw new RuntimeException("AutoScanServiceImpl-视频审核保存APP端失败!");
        }

        videoTaskService.addToTask(video.getId(), video.getPublishTime());
        return R.success("发布成功");

    }

    

    @Resource
    private IUserClient userClient;
    /**
     * 保存或修改文章
     * @param
     */
    private void saveOrUpdateVideo(Video video) {
        //补全属性
        Long userId = AppThreadLocalUtil.getUser().getId();

//        Video.setAuthorName(user.getName());
        video.setAuthorId(userId);
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(userId).getData();
        User userInfoDto = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);

        VideoChannel channel = videoChannelService.query().eq("id", video.getChannelId()).one();
        video.setChannelName(channel.getChannelName());
        video.setCreatedTime(new Date());
        video.setPublishTime(new Date());
        video.setEnable((short)1);//默认上架
        if(video.getId() == null){
            //保存
            save(video);
        }

    }


}
